﻿using System.Collections.Generic;
using BlogML.Xml;
using NGM.BlogML.Models;
using Orchard.ContentManagement;

namespace NGM.BlogML.Services.Stratagies {
    public class CommentsImportStratagy : IMultipleImportStratagy {
        private readonly IEnumerable<IImportStratagy> _importStratagies;

        public CommentsImportStratagy(IEnumerable<IImportStratagy> importStratagies) {
            _importStratagies = importStratagies;
        }

        public bool IsType(object objectToImport) {
            return objectToImport is BlogMLPost.CommentCollection;
        }

        public ContentItem Import(ImportSettings importSettings, BlogMLBlog blog, object objectToImport, IContent parentContent) {
            var commentsToImport = (BlogMLPost.CommentCollection)objectToImport;

            foreach (var commentToImport in commentsToImport) {
                ImportAdditionalContentItems(importSettings, blog, commentToImport, parentContent);
            }

            return null;
        }

        public void ImportAdditionalContentItems<T>(ImportSettings importSettings, BlogMLBlog blog, T objectToImport, IContent parentContent) {
            foreach (var importStratagy in _importStratagies) {
                if (importStratagy.IsType(objectToImport)) {
                    importStratagy.Import(importSettings, blog, objectToImport, parentContent);
                }
            }
        }
    }
}